#pragma once

#include <sys/ptrace.h>
#include <sys/user.h>
#include <signal.h>

#include "cfd-filter.h"

/* Really weird stuff.. Why + 2? I don't know. This just works..
 * FIXME. If you know something :] */

#ifndef __x86_64__
#define DEFAULT_BASE 0x08048000 + 2
#else
#define DEFAULT_BASE 0x00400000 + 2
#endif

typedef void (* handle_error) (pid_t pid
                               , int signal_value
                               , struct user_regs_struct * error_registers
                               , struct user_regs_struct * orig_registers
                               , void * text
                               , long   text_size
                               , long   text_addr
                               );

int load_text(pid_t pid
              , long offset
              , void * text
              , long text_size
              );

int store_text(pid_t pid
              , long offset
              , void * text
              , long text_size
              );

#define load_data load_text
#define store_data store_text

int run_text(pid_t pid
             , void          * text
             , long            text_size
             , void          * data
             , long            data_size
             , struct filter * filter
             , handle_error    handler
             );

/* Not crossplatform */

/* FIXME: Add other archs */
#ifdef __x86_64__
#define R_DATA  rsp
#define R_PC    rip
#define R_0     rax
#define R_1     rbx
#define R_2     rcx
#define R_3     rdx
#else
#define R_DATA  esp
#define R_PC    eip
#define R_0     eax
#define R_1     ebx
#define R_2     ecx
#define R_3     edx
#endif

